package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.RecyclerDataBean;
import com.ohos.carbon.provider.RecyclerViewProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class RecyclerViewSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title;
    private ListContainer listContainer;
    private List<RecyclerDataBean> mLists;
    private RecyclerViewProvider provider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_recycler_view);
        initRecycData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("RecyclerView")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("RecyclerView")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("RecyclerView", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("RecyclerView", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("RecyclerView", 1);
            pf.flushSync();
        }
    }

    private void initRecycData() {
        mLists = new ArrayList<>();
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Strawberry", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Apple", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Orange", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Lemon", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Beer", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Lime", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Watermelon", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Blueberry", ResourceTable.Media_recycler_image1));
        mLists.add(new RecyclerDataBean(ResourceTable.Media_checkbox_false1, "Plum", ResourceTable.Media_recycler_image1));

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_Recyc_list);
        text_title.setText("RecyclerView");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        provider = new RecyclerViewProvider(mLists, this);
        listContainer.setItemProvider(provider);
        listContainer.setItemClickedListener((listContainer, component, i, l) -> {
            RecyclerDataBean bean0 = mLists.get(0);
            RecyclerDataBean bean1 = mLists.get(1);
            RecyclerDataBean bean2 = mLists.get(2);
            RecyclerDataBean bean3 = mLists.get(3);
            RecyclerDataBean bean4 = mLists.get(4);
            RecyclerDataBean bean5 = mLists.get(5);
            RecyclerDataBean bean6 = mLists.get(6);
            RecyclerDataBean bean7 = mLists.get(7);
            RecyclerDataBean bean8 = mLists.get(8);
            provider.notifyDataChanged();
            switch (i) {
                case 0:
                    bean0.setIcon(ResourceTable.Media_checkbox_true1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 1:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_true1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 2:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_true1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 3:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_true1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 4:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_true1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 5:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_true1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 6:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_true1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 7:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_true1);
                    bean8.setIcon(ResourceTable.Media_checkbox_false1);
                    provider.notifyDataChanged();
                    break;
                case 8:
                    bean0.setIcon(ResourceTable.Media_checkbox_false1);
                    bean1.setIcon(ResourceTable.Media_checkbox_false1);
                    bean2.setIcon(ResourceTable.Media_checkbox_false1);
                    bean3.setIcon(ResourceTable.Media_checkbox_false1);
                    bean4.setIcon(ResourceTable.Media_checkbox_false1);
                    bean5.setIcon(ResourceTable.Media_checkbox_false1);
                    bean6.setIcon(ResourceTable.Media_checkbox_false1);
                    bean7.setIcon(ResourceTable.Media_checkbox_false1);
                    bean8.setIcon(ResourceTable.Media_checkbox_true1);
                    provider.notifyDataChanged();
                    break;

            }
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
